//
//  jobpagination.m
//  ehubApp
//
//  Created by AonePOS on 24/09/13.
//  Copyright (c) 2013 AonePOS. All rights reserved.
//

#import "Customerpagination.h"

@implementation Customerpagination

@synthesize completeresultset,activeresultset,paginationsegment,displayedresults,resultsforpage,totalnumberofsegments,prevs,next,segmentArray;

-(void)paginationinitializer :(NSMutableArray *)resultsarray :(NSInteger)resultsperpage
{
    NSInteger totalnumberofresults;
    self.resultsforpage=resultsperpage;
    [self.completeresultset arrayByAddingObjectsFromArray:resultsarray];
    
    totalnumberofresults=[self.completeresultset count];
    
    int count=0;
    
    if (totalnumberofresults%resultsperpage!=0)
    {
        totalnumberofsegments=(totalnumberofresults/resultsperpage)+1;
    }
    else{
        totalnumberofsegments=(totalnumberofresults/resultsperpage);
    }
    count=totalnumberofsegments;
    //segmentArray=nil;
    segmentArray=[[NSMutableArray alloc] init];
    
    for (int i=1; i<=count; i++)
    {
        NSString *myNewString = [NSString stringWithFormat:@"%d", i];
        
        [segmentArray insertObject:myNewString atIndex:(i-1)];
    }
    
    
    paginationsegment=[[UISegmentedControl alloc]initWithItems:segmentArray];
     [paginationsegment setBackgroundImage:[UIImage imageNamed:@"top-tabs-normal.png"] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
    
}
-(NSMutableArray *)activateResults :(NSInteger)activeSegmentIndex

{
    NSInteger totalnumberofresults;
    
    
    totalnumberofresults=[self.completeresultset count];
    
    if(activeSegmentIndex>totalnumberofsegments){activeSegmentIndex=totalnumberofsegments;}
    //NSLog(@"%d - active segment index value",activeSegmentIndex);
    NSMutableArray *returnResults=[[NSMutableArray alloc]init];
    
    minimumval=((activeSegmentIndex)*resultsforpage)+1;
    //rangeval=
    if([self.completeresultset count]<=resultsforpage)
    {
        maximumval=self.completeresultset.count;
        rangeval=0;
    }
    else
    {
        rangeval=resultsforpage-1;
        maximumval=minimumval+ rangeval;
        if(maximumval>[self.completeresultset count])
        {
            maximumval=[self.completeresultset count];
        }
    }
    NSUInteger j=0;
    self.activeresultset=Nil;
    for(NSUInteger i=0;i<totalnumberofresults;i++)
    {        if(     ((i+1)>=minimumval) && ((i+1)<=maximumval)  )
    {
        
        NSMutableArray *completeArray=[self.completeresultset objectAtIndex:i];
        
        [returnResults insertObject:completeArray  atIndex:j] ;
        j++;
    }
    }
    [self setActiveresultset:returnResults];
    //NSLog(@" active results %@",activeresultset);
    return returnResults;
    
}
-(NSString *)returnvalueforkey :(NSMutableArray *)resultsArray  :(NSMutableArray *)paginationActiveResultSet :(NSString *)keyvalue
{
    NSDictionary *titlesDict;
    NSString  *valueString;
    NSString  *keyString;
    NSMutableArray *consultantIdsArray;//=[[NSMutableArray alloc] init];
    consultantIdsArray=[[NSMutableArray alloc] init];
    
    for (int j=0 ; j<[resultsArray count] ; j++)
    {
        NSMutableArray *DictsArray=[paginationActiveResultSet objectAtIndex:j];
        
        
        for (int i=0; i<[DictsArray count]; i++)
        {
            
            titlesDict=[DictsArray objectAtIndex:i];
            
            
            keyString=[titlesDict objectForKey:@"key"];
            if ([keyString isEqualToString:keyvalue])
            {
                valueString=[titlesDict objectForKey:@"value"];
                [consultantIdsArray addObject:valueString];
                return valueString;
            }
            
        }
        
        
    }
    //NSLog(@"%@ consultants",consultantIdsArray);
    return Nil;
}
-(void)paginationinitializer :(NSMutableArray *)resultsarray{
    [self paginationinitializer:resultsarray :10];
    
}


@end
